package com.ma.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ma.auth.mapper.SysRoleMapper;
import com.ma.auth.service.SysRoleService;
import com.ma.auth.service.SysUserRoleService;
import com.ma.model.system.SysRole;
import com.ma.model.system.SysUserRole;
import com.ma.vo.system.AssginRoleVo;
import com.ma.vo.system.SysRoleQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {
    @Autowired
    private SysUserRoleService userRoleService;
    @Override
    public Page<SysRole> limit(Long page, Long limit, SysRoleQueryVo sysRoleQueryVo) {
        Page<SysRole> pageParam = new Page<>(page,limit);
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        String roleName = sysRoleQueryVo.getRoleName();
        if(!StringUtils.isEmpty(roleName)){
            wrapper.like(SysRole::getRoleName,roleName);
        }
        return page(pageParam, wrapper);
    }

    @Override
    public Map<String, Object> findRoleDateByUserId(Long userId) {
        List<SysRole> allRoles = baseMapper.selectList(null);
        //根据userid查询角色
        LambdaQueryWrapper<SysUserRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUserRole::getUserId,userId);
        List<SysUserRole> existUserRoleList = userRoleService.list(queryWrapper);
        //通过查询得到的用户所属角色列表获取到角色id
        List<Long> existRoleId = existUserRoleList.stream().map(c -> c.getRoleId()).collect(Collectors.toList());
        //根据角色id查询角色信息
        List<SysRole> assignRole = new ArrayList<>();
        for(SysRole sysRole : allRoles){
            if(existRoleId.contains(sysRole.getId())){
                assignRole.add(sysRole);
            }
        }
        //返回所有角色信息及用户所属角色信息
        Map<String,Object> res = new HashMap<>();
        res.put("allRoles",allRoles);
        res.put("assignRoles",assignRole);
        return res;
    }

    @Override
    public void doAssign(AssginRoleVo assginRoleVo) {
        //将用户之前的所属角色进行删除
        LambdaQueryWrapper<SysUserRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUserRole::getUserId,assginRoleVo.getUserId());
        userRoleService.remove(queryWrapper);
        //为用户分配新的角色信息
        List<Long> roleIdList = assginRoleVo.getRoleIdList();
        for(Long roleId : roleIdList){
           if(StringUtils.isEmpty(roleId)){
               continue;
           }
           SysUserRole sysUserRole = new SysUserRole();
           sysUserRole.setRoleId(roleId);
           sysUserRole.setUserId(assginRoleVo.getUserId());
           userRoleService.save(sysUserRole);
        }

    }
}
